package com.baibei.ebec.moments.adapter;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.baibei.ebec.home.R;
import com.baibei.widget.FixedEditText;
import com.blankj.utilcode.utils.ToastUtils;

import org.json.JSONObject;
import org.w3c.dom.Text;

import java.util.ArrayList;

/**
 * Created by tang on 2018/5/22.
 */

public class CommentsAadapter extends BaseExpandableListAdapter {
    private ArrayList<JSONObject> groupList;
    private ArrayList<ArrayList<JSONObject>> childList;
    private FixedEditText etComment;
    public CommentsAadapter(ArrayList<JSONObject> groupList, ArrayList<ArrayList<JSONObject>> childList,FixedEditText etComment) {
        this.groupList = groupList;
        this.childList = childList;
        this.etComment = etComment;
    }

    @Override
    public int getGroupCount() {
        return groupList.size();
    }

    @Override
    public int getChildrenCount(int i) {
        if (childList.get(i) == null) {
            return 0;
        } else {
            return childList.get(i).size();
        }
    }

    @Override
    public Object getGroup(int i) {
        return groupList.get(i);
    }

    @Override
    public Object getChild(int i, int i1) {
        if (childList.get(i) == null) {
            return null;
        } else {
            return childList.get(i).get(i1);
        }
    }

    @Override
    public long getGroupId(int i) {
        return i;
    }

    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(final int i, boolean b, View view, ViewGroup viewGroup) {
        GroupViewHolder groupViewHolder;
        if (view == null) {
            view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_expandlist_group, viewGroup, false);
            groupViewHolder = new GroupViewHolder();
            groupViewHolder.userName = view.findViewById(R.id.tv_username);
            groupViewHolder.content = view.findViewById(R.id.tv_content);
            groupViewHolder.time = view.findViewById(R.id.tv_time);
            view.setTag(groupViewHolder);
        } else {
            groupViewHolder = (GroupViewHolder) view.getTag();
        }
        final String groupUserName = groupList.get(i).optString("nickName");
        groupViewHolder.userName.setText(groupUserName);
        groupViewHolder.time.setText(groupList.get(i).optString("createDate"));
        groupViewHolder.content.setText(groupList.get(i).optString("content"));
        groupViewHolder.content.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                etComment.getText().clear();
                etComment.setFixedText("");
                etComment.setHint("回复 "+groupUserName+":");
                int[] ints = new int[]{1,i};
                etComment.setTag(ints);
            }
        });
        return view;
    }

    @Override
    public View getChildView(final int i, final int i1, boolean b, View view, ViewGroup viewGroup) {
        ChildViewHolder childViewHolder;
        if (view == null) {
            view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_expandlist_child, viewGroup, false);
            childViewHolder = new ChildViewHolder();
            childViewHolder.userName = view.findViewById(R.id.tv_comment_id);
            childViewHolder.content = view.findViewById(R.id.tv_comment_content);
            view.setTag(childViewHolder);
        } else {
            childViewHolder = (ChildViewHolder) view.getTag();
        }
        if (childList.get(i) == null) {
            return null;
        }
//        Log.i("commentsss",childList.get(i).toString());
        final String childUserName = childList.get(i).get(i1).optString("nickName");
        childViewHolder.userName.setText(childUserName + ":");
        childViewHolder.content.setText(childList.get(i).get(i1).optString("content"));
        childViewHolder.content.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                etComment.getText().clear();
                etComment.setHint(null);
                etComment.setFixedText("回复 "+childUserName+":");
                int[] ints = {2, i};
                etComment.setTag(ints);
            }
        });
        return view;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }

    public static class GroupViewHolder {
        ImageView avatar;
        TextView userName;
        TextView time;
        TextView content;
    }

    public static class ChildViewHolder {
        TextView userName;
        TextView content;
    }
}
